package com.qzimp.fujiancuo.interceptors;

import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.JWTValidator;

import com.qzimp.fujiancuo.entity.vo.ResultCode;
import com.qzimp.fujiancuo.exceptions.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {
    Logger logger= LoggerFactory.getLogger(TokenInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token=request.getHeader("Authorization");
        System.out.println(token);


        try {
            JWTValidator.of(token).validateDate(DateUtil.date());
            boolean verify=JWTUtil.verify(token,"abcdef".getBytes());
            if(verify){
                return  true;
            }
            else {
                throw  new ApiException(ResultCode.USER_TOKEN_VERIFY_FAILURE);
            }
        }
        catch (Exception e){

            throw new ApiException(ResultCode.USER_TOKEN_VERIFY_DATE_FAILURE);
        }



    }
}
